package com.vanzo.some.action;

import android.app.Instrumentation;
import android.os.SystemClock;
import android.view.MotionEvent;

import java.util.List;

/**
 * Created by tisen on 2017/6/9.
 */

public class ActionRunnable implements Runnable {
    private HandAction action;
    private int index =0;
    private List<HandAction>actions;
    private Instrumentation instrumentation;
    public ActionRunnable() {
    }

    public ActionRunnable(List<HandAction> actions, Instrumentation instrumentation) {
        this.actions = actions;
        this.instrumentation = instrumentation;
    }

    @Override
    public void run() {
        action = actions.get(index);
        try {
            Thread.sleep(action.getDelay());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int action_event = action.getAction() == 0 ? MotionEvent.ACTION_DOWN : MotionEvent.ACTION_UP;
        instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), action_event, action.getX(), action.getY(), 0));
        index++;
        if(actions.size()>index){
            run();
        }else {

        }
    }
}
